package controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import entity.ActionType;
import entity.Comment;
import entity.Message;
import entity.MessageResult;
import entity.Note;
import entity.User;
import net.sf.json.JSONObject;
import service.ActionTypeService;
import service.CommentService;
import service.MessageService;
import service.NoteService;
import service.UserService;
import util.ResponseUtil;
import util.ResultFormatUtil;

@Controller
@RequestMapping("/message")
public class MessageController {
	@Resource
	private NoteService noteService;
	@Resource
	private CommentService commentService;
	@Resource
	private UserService userService;
	@Resource
	private ActionTypeService actionTypeService;
	@Resource
	private MessageService messageService;
	//http://localhost:8080/CampusApp/message/getMessageListByUserId.do?userId=1
	@RequestMapping("/getMessageListByUserId")
	public void getMessageListById(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String user_Id = request.getParameter("userId");
		int userId=Integer.parseInt(user_Id);
		List<Message> messages=new ArrayList<>();
		List<Note> notes=noteService.getNoteListByUserId(userId);
		for(int i=0;i<notes.size();i++){
			Note note = notes.get(i);
			int noteId=note.getNoteId();
			List<Message> messages2=messageService.getMessageByNoteId(noteId);
			messages.addAll(messages2);
		}
		//
		List<MessageResult> messageResults=new ArrayList<>();
		for(int i=0;i<messages.size();i++){
			Message message = messages.get(i);
			MessageResult messageResult = new MessageResult();
			messageResult.setMessage(message);
			int actionId=message.getActionId();
			ActionType actionType=actionTypeService.getActionTypeByActionId(actionId);
			messageResult.setAction(actionType);
			int commentId = message.getCommentId();
			Comment comment=commentService.getCommentByCommentId(commentId);
			messageResult.setComment(comment);
			int noteId = message.getNoteId();
			Note noteId2 = noteService.getNoteByNoteId(noteId);
			messageResult.setNote(noteId2);
			User commentEditor=userService.getUserByIdNoPwd(comment.getEditorId());
			messageResult.setCommentEditor(commentEditor);
			User noteEditor=userService.getUserByIdNoPwd(noteId2.getEditorId());
			messageResult.setNoteEditor(noteEditor);
			messageResults.add(messageResult);
			
		}
		JSONObject result = ResultFormatUtil.getResultFrom(true, "messageList获取成功", messageResults);
		ResponseUtil.write(response, result);
//	}else{
//		JSONObject result = ResultFormatUtil.getResultFrom(false, "noteList获取失败", null);
//		ResponseUtil.write(response, result);
//	}
		
	}
	// http://localhost:8080/CampusApp/message/deleteMessageByMessageId.do?messageId=8
	@RequestMapping("/deleteMessageByMessageId")
	public void deleteMessageByMessageId(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String message_Id = request.getParameter("messageId");
		int messageId=Integer.parseInt(message_Id);
		if(messageId>0){
			int count=messageService.deleteMessageByMessageId(messageId);
			if(count>0){
				JSONObject result = ResultFormatUtil.getResultFrom(true, "删除Message成功", null);
				ResponseUtil.write(response, result);
			}else{
				JSONObject result = ResultFormatUtil.getResultFrom(false, "删除Message失败", null);
				ResponseUtil.write(response, result);
			}
		}else{
			JSONObject result = ResultFormatUtil.getResultFrom(false, "删除Message失败", null);
			ResponseUtil.write(response, result);
		}
		
	}
	
}
